// pages/zhoufangxuanze/zhoufangxuanze.js

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mealsList: '',
    GRADE: '',
    flag: true,


    startdate: '',
    enddate: '',
    minDate: new Date(2024, 4, 1).getTime(), //可选最小日期

    display: false, //日历
  },


  /* 查询日期 */
  async onConfirm(event) {

    this.setData({

      mealsList: app.globalData.mealsList,

    })


    const [start, end] = event.detail;
    console.log(event.detail);
    const kaishi = new Date(start).setHours(0, 0, 0, 0);
    const jieshu = new Date(end).setHours(23, 59, 59, 999);
    // const  starttime = util.formatDateNy(kaishi) 
    // const  endtime =  util.formatDateNy(jieshu)



    console.log(kaishi);
    console.log(jieshu);
    this.setData({
      startdate: kaishi,
      enddate: jieshu,
      // page:1,
      // serchTime:`${starttime}至${endtime}`
    })

    if (jieshu - kaishi > 1382400000) {

      wx.showModal({
        title: '温馨提示',
        content: '导出大于16天的数据请联系管理员',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后   
            console.log('用户点击确定')
          } else { //这里是点击了取消以后
            console.log('用户点击取消')
          }
        }
      })


    } else {
      this.setData({
        mealsList: app.globalData.mealsList,
      })
  
  


      if (this.data.flag) {
        this.data.flag = false


          wx.request({
            url: app.globalData.interface+'/Test/API_visiting_download',
            method: 'get',
            data: {
              EMPLOYEE_ID: this.data.mealsList, //ID
              START_TIME: this.data.startdate,
              END_TIME: this.data.enddate,
              id: 61000089
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: res => {
              console.log(res);

              console.log(res.data.data);
              if (res.data.require == "success") {
                wx.showModal({
                  title: "导出失败",
                })
              } else {
                console.log('res', res.data.data);
                this.setData({
                  schoolexsls: res.data.data,
                  display: false, //日历
                })


           
            
                this.exportdao()


              }
            }
          })

        setTimeout(() => {
          this.data.flag = true
        }, 5000)

      }




    }







    // this.onewyyj( 
    //   this.data.startdate,
    //  this.data.enddate
    //  )

  },

  onCe() {
    this.setData({
      display: false
    });
  },









  back() {
    wx.navigateBack({
      delta: 1,
    })
  },


  //  // 导出
  exportData() {
    this.setData({
      display: true, //日历
    })
  },



  // 导出
  exportdao() {
    let that = this
    const auth = wx.getStorageSync("auth");
    wx.showLoading({
      title: "加载中",
    });
    wx.downloadFile({
      //下载
      url: that.data.schoolexsls,
      header: {
        "content-type": "application/vnd.ms-excel",
        auth: auth,
      },
      success(res) {
        console.log(res);
        if (res.statusCode === 200) {
          let savePath =
            wx.env.USER_DATA_PATH + "/TESUN_PLAN_" + Date.now() + '.xlsx';
          wx.getFileSystemManager().saveFile({
            //下载成功后保存到本地
            tempFilePath: res.tempFilePath,
            filePath: savePath,
            success(res) {
              wx.hideLoading();
              console.log(res);
              let savePath = res.savedFilePath;
              wx.showModal({
                title: "下载成功",
                content: "是否打开?",
                confirmColor: "#0bc183",
                confirmText: "打开",
                success(res) {
                  if (res.confirm) {
                    wx.openDocument({
                      //打开
                      filePath: savePath,
                      showMenu: true,
                      success(res) {
                        console.log(res);
                      },
                    });
                  } else if (res.cancel) {}
                },
              });
            },
            fail(err) {
              wx.hideLoading();
              wx.showModal({
                title: '温馨提示',
                content: that.data.schoolexsls,
                showCancel: false, //是否显示取消按钮
                success: function (result) {}
              })
            },
          });
        } else {
          wx.hideLoading();
          wx.showModal({
            title: '温馨提示',
            content: that.data.schoolexsls,
            showCancel: false, //是否显示取消按钮
            success: function (result) {}
          })
        }
      },
      fail(err) {
        wx.hideLoading();
        wx.showModal({
          title: '温馨提示',
          content: that.data.schoolexsls,
          showCancel: false, //是否显示取消按钮
          success: function (result) {}
        })
      }
    });
  },














  weiyou() {
    wx.navigateTo({
      url: '/pages/zhoufangweiyou/zhoufangweiyou',
    })
  },

  feiweiyou() {
    wx.navigateTo({
      url: '/pages/zoufnagxitong/zoufnagxitong',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    console.log(e.data);

    this.setData({
      GRADE: e.data

    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})